<?php
/**
 *	description:ZMAXWECHAT 消息队列状态
 *  author：min.zhang
 *  Email:zhang19min88@163.com
 *	Url:http://www.zmax99.com
 *  copyright:南宁市程序人软件科技有限责任公司保留所有权利
 *  date:2016-05-03
 * @license GNU General Public License version 3, or later
 *  chcker :min.zhang 
 *  chcek date:2016-05-26
 */

defined('_JEXEC') or die('Restricted Access');
jimport('joomal.form.fields.radio');

class JFormFieldNewsqueue extends JFormFieldRadio
{
	/**
	 * The form field type.
	 *
	 * @var    string
	 * @since  11.1
	 */
	protected $type = 'Newsqueue';

	/**
	 * Method to get the field label markup.
	 *
	 * @return  string  The field label markup.
	 *
	 * @since   11.1
	 */
	protected function getLabel()
	{
		if (empty($this->element['label']) && empty($this->element['description']))
		{
			return '';
		}

		$title = $this->element['label'] ? (string) $this->element['label'] : ($this->element['title'] ? (string) $this->element['title'] : '');
		$heading = $this->element['heading'] ? (string) $this->element['heading'] : 'h4';
		$description = (string) $this->element['description'];
		$class = !empty($this->class) ? ' class="' . $this->class . '"' : '';


		$html = array();
		$html[] = !empty($title) ? '<' . $heading . '>' . JText::_($title) . '</' . $heading . '>' : '';
		$html[] = !empty($description) ? JText::_($description) : '';
		
		return '</div><div ' . $class . '>' . implode('', $html);
	}

	/**
	 * Method to get the field input markup.
	 *
	 * @return  string  The field input markup.
	 *
	 * @since   11.1
	 */
	protected function getInput()
	{
		$this->testLimit();
		$html = $this->getSendLimit();
		$html .=$this->getNewsqueue();
		
		$senderBtn="";
		if($this->testLimit())
		{
			$senderBtn = parent::getInput();	
		}
		
		return '<div class="row-fluid">'.$html."</div>".$senderBtn;
	}
	
	protected function getSendLimit()
	{
		
		if($this->testLimit())
		{
			$text='<p class="alert alert-info">今日你还可以向微信群发一条消息</p>';	
		}
		else
		{
			$text='<p class="alert alert-danger">你今日的推送额度已经用完了，无法执行推送!</p>';
		}
		$html ='<div>
					<h4>推送额度<small>你今天可执行推送消息的次数</small></h4>
					'.$text.'
				</div>';
		return $html;
	}
	
	protected function testLimit()
	{
		$today = date("Y-m-d" ,time());
		$tomorrow = date("Y-m-d" ,strtotime("+1 day"));
		$today = JDate::getInstance($today)->toSql();
		$tomorrow =JDate::getInstance($tomorrow)->toSql();
		
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select("id")->from("#__zmaxwechat_sended");
		$query->where('date BETWEEN "'.$today.'" AND "'.$tomorrow.'"');
		$db->setQuery($query);
		$item = $db->loadObject();
		if($item)
		{
			return 0;
		}
		return 1;
	}
	protected function _getNewsqueue()
	{
		 $db = JFactory::getDBO();
		 $query = $db->getQuery(true);
		 $query->select("newsqueue_id")->from("#__zmaxwechat_news_map");
		 $db->setQuery($query);
		 $ids = $db->loadcolumn();
		 if(is_array($ids) AND $ids)
		 {
			 $ids = implode(",",$ids);
		 }

		$query = $db->getQuery(true);
		$query->select('n.id ,n.extension ,n.create_date,n.ordering,n.top');
		$query->from('#__zmaxwechat_newsqueue AS n');
		if($ids)
		{
			$query->where('n.id NOT IN ('.$ids.')');	
		}
		$query->select('c.title AS title');
		$query->leftJoin("#__content as c ON c.id = n.article_id");
		
		$orderCol = 'id';
		$orderDirn = 'desc';
		$query->order($db->escape($orderCol).' '.$db->escape($orderDirn));
		
		$query->setLimit(8);
		
		$db->setQuery($query);
		$news = $db->loadObjectList();
		return $news;
	}
	
	protected function getNewsqueue()
	{
		JHtml::_('jquery.framework');
		$doc = JFactory::getDocument();
		$doc->addScript(JUri::root()."administrator/components/com_zmaxwechat/js/newsqueue.js");
		
		$news = $this->_getNewsqueue();
		$head="<div>
					<h4>图文队列<small>在图文队列里的文章</small></h4>
				</div>";
		$html=$head;
		if(empty($news))
		{
			$html.="<div class='alert'>当前队列中没有任何的文章</div>";
			return $html;
		}
		
		$tableHead='
			<div class="row-fluid">
			    <div class="span6">
					<table class="table table-bordered">
						<thead>
							<th>文章</th>
							<th>所属组件</th>
							<th>加入时间</th>
							<th>操作</th>
						</thead>
						<tbody>';
		$tableFoot='	
						</tbody>
					</table>
				</div>
			</div>';
		$tableBody="";
		
		foreach($news as $new)
		{
			$tableBody=$tableBody.'<tr id="new_'.$new->id.'">';
			$tableBody=$tableBody.'<td>'.$new->title.'</td>';
			$tableBody=$tableBody.'<td>'.$this->formatExtension($new->extension).'</td>';
			$tableBody=$tableBody.'<td>'.$new->create_date.'</td>';
			$tableBody=$tableBody.'<td><a href="javascript:void(0);" class="deleteNew" data="'.$new->id.'">删除</a></td>';
			$tableBody=$tableBody.'</tr>';
		}
		
		$html .= $tableHead.$tableBody.$tableFoot;
		return $html;
	}
	
	public function formatExtension($extension)
	{
		$extension = strtolower($extension);
		switch($extension)
		{
			case 'com_content':
						$extension="文章组件";break;
			case 'com_k2':
						$extension = "K2组件";break;
			default:				
						$extension =  "文章组件";break;
		}
		return $extension ;
	}
}
